//
//  ProfileView.swift
//  WechatApp
//
//  Created on 2024.
//

import SwiftUI
import UIKit

struct ProfileView: View {
    let name: String
    let avatarUrl: String?
    let wechatId: String?
    
    init(name: String, avatarUrl: String?, wechatId: String? = nil) {
        self.name = name
        self.avatarUrl = avatarUrl
        self.wechatId = wechatId ?? "wxid_123456"
    }
    
    var body: some View {
        ScrollView {
            VStack(spacing: 0) {
                // Header
                HStack(alignment: .top, spacing: 20) {
                    if let avatarUrl = avatarUrl {
                        if avatarUrl.hasPrefix("img_"), let image = UIImage(named: avatarUrl) {
                            Image(uiImage: image)
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .frame(width: 64, height: 64)
                                .clipShape(RoundedRectangle(cornerRadius: 6))
                        } else if let url = URL(string: avatarUrl) {
                            AsyncImage(url: url) { image in
                                image
                                    .resizable()
                                    .aspectRatio(contentMode: .fill)
                            } placeholder: {
                                InitialsAvatar(name, size: 64)
                            }
                            .frame(width: 64, height: 64)
                            .clipShape(RoundedRectangle(cornerRadius: 6))
                        } else {
                            InitialsAvatar(name, size: 64)
                        }
                    } else {
                        InitialsAvatar(name, size: 64)
                    }
                    
                    VStack(alignment: .leading, spacing: 4) {
                        Text(name)
                            .font(.system(size: 22, weight: .bold))
                        
                        Text("WeChat ID: \(wechatId ?? "wxid_123456")")
                            .font(.system(size: 14))
                            .foregroundColor(.secondary)
                        
                        Text("Region: China")
                            .font(.system(size: 14))
                            .foregroundColor(.secondary)
                    }
                }
                .padding(20)
                
                Divider()
                
                // Body items
                ProfileRow(title: "Set Remarks and Tags")
                Divider()
                    .padding(.leading, 16)
                ProfileRow(title: "Friend Permissions")
                
                Spacer()
                    .frame(height: 8)
                
                ProfileRow(title: "Moments", trailing: AnyView(MomentsPreview()))
                Divider()
                    .padding(.leading, 16)
                ProfileRow(title: "More Info")
                
                Spacer()
                    .frame(height: 8)
                
                ProfileRow(title: "Messages and Call")
                
                Spacer()
                
                // Footer buttons
                VStack(spacing: 12) {
                    Button(action: {
                        // Navigate to chat
                    }) {
                        HStack {
                            Image(systemName: "message")
                            Text("Send Message")
                                .fontWeight(.bold)
                        }
                        .foregroundColor(Color(red: 0.34, green: 0.42, blue: 0.58))
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 12)
                        .background(Color(red: 0.97, green: 0.97, blue: 0.97))
                        .clipShape(RoundedRectangle(cornerRadius: 4))
                    }
                    
                    Button(action: {}) {
                        HStack {
                            Image(systemName: "video.fill")
                            Text("Audio/Video Call")
                                .fontWeight(.bold)
                        }
                        .foregroundColor(Color(red: 0.34, green: 0.42, blue: 0.58))
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 12)
                        .background(Color(red: 0.97, green: 0.97, blue: 0.97))
                        .clipShape(RoundedRectangle(cornerRadius: 4))
                    }
                }
                .padding(16)
            }
        }
        .background(Color.white)
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            ToolbarItem(placement: .navigationBarTrailing) {
                Button(action: {}) {
                    Image(systemName: "ellipsis")
                }
            }
        }
    }
}

struct ProfileRow: View {
    let title: String
    var trailing: AnyView? = nil
    
    var body: some View {
        HStack {
            Text(title)
                .font(.system(size: 16))
            
            Spacer()
            
            if let trailing = trailing {
                trailing
            }
            
            Image(systemName: "chevron.right")
                .font(.system(size: 12))
                .foregroundColor(.secondary)
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 12)
        .background(Color.white)
    }
}

struct MomentsPreview: View {
    var body: some View {
        HStack(spacing: 4) {
            if let img1 = UIImage(named: "img_1") {
                Image(uiImage: img1)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 40, height: 40)
                    .clipShape(RoundedRectangle(cornerRadius: 4))
            }
            
            if let img2 = UIImage(named: "img_2") {
                Image(uiImage: img2)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 40, height: 40)
                    .clipShape(RoundedRectangle(cornerRadius: 4))
            }
            
            if let img3 = UIImage(named: "img_3") {
                Image(uiImage: img3)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 40, height: 40)
                    .clipShape(RoundedRectangle(cornerRadius: 4))
            }
        }
    }
}

#Preview {
    NavigationView {
        ProfileView(name: "张三", avatarUrl: nil)
    }
}

